package com.cskaoyan.shiro;

import com.cskaoyan.utils.StringUtils;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.stereotype.Component;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;

/**
 * 类<code>Doc</code>用于：TODO
 *
 * @author Acher
 * @version 1.0
 * @date 2021-10-20
 */
@Component
public class CustomWebSessionManager extends DefaultWebSessionManager {

    private static final String ADMIN_TOKEN = "X-CskaoyanMarket-Admin-Token";
    private static final String USER_TOKEN = "X-CskaoyanMarket-Token";

    @Override
    protected Serializable getSessionId(ServletRequest request, ServletResponse response) {
        HttpServletRequest req = (HttpServletRequest) request;
        String adminSessionId = req.getHeader(ADMIN_TOKEN);
        if (!StringUtils.isEmpty(adminSessionId)) return adminSessionId;

        String userSessionId = req.getHeader(USER_TOKEN);
        if (!StringUtils.isEmpty(userSessionId)) return userSessionId;

        return super.getSessionId(request, response);
    }
}
